<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Translate extends Admin_Controller {

    public $data;

    function __construct() {
        parent::__construct();
        $this->load->model(array("Mtranslate","Mlanguage"));
        $this->breadcrumbs->push('Translate', 'admincp/translate/view');
    }

    public function view() {
		$dataLang = $this->registry->get('language');
		$maxLang = count($dataLang);
    	if($this->input->is_ajax_request()){
            $start = $this->input->post('iDisplayStart');
            $length = $this->input->post('iDisplayLength');
            $selectBeforeFrom = '';
            $selectAfterFrom = '';
            $where = '1=1';
            $i=1;
            foreach($dataLang as $lang){
            	$i_next = $i - 1;
            	if($i == 1){
            		$selectBeforeFrom .= 'a1.tran_id, a1.word as keyword, a'.$i.'.translate as translate_'.$lang['lang_code'];
            		$selectAfterFrom .= '(select * from tpt_translate where lang_code = "'.$lang['lang_code'].'") a1';
            		// $where .= ' AND a1.tran_id = a'.$i_next.'.tran_id';
            	}else{
                    // if($i == $maxLang){
                    //     $i_next = 1;
                    // }
            		$selectBeforeFrom .= ', a'.$i.'.translate as translate_'.$lang['lang_code'];	
            		$selectAfterFrom .= ' LEFT JOIN (select * from tpt_translate where lang_code = "'.$lang['lang_code'].'") a'.$i.' ON a'.$i.'.tran_id = a'.$i_next.'.tran_id';
            		
	            	// $where .= ' AND a'.$i.'.tran_id = a'.$i_next.'.tran_id';
            	}
            	$i++;
            }
            $sql = "select SQL_CALC_FOUND_ROWS ".$selectBeforeFrom." from ".$selectAfterFrom."
where ".$where." ORDER BY a1.tran_id DESC LIMIT $start, $length";
            $data['list_translate'] = $this->db->query($sql)->result_array();
            $sql = "SELECT FOUND_ROWS() AS total";
            $rows = $this->db->query($sql)->row_array();

            if ((isset($data['list_translate']) == TRUE) && ($data['list_translate'] != '') && (is_array($data['list_translate']) == TRUE)) {
                $aaData = array();                
                foreach ($data['list_translate'] as $key => $value) {
                    $aaData[$key][] = '<td>' . $value['keyword'] . '</td>';
                    foreach($dataLang as $lang){
                    	$aaData[$key][] = '<td>' . $value['translate_'.$lang['lang_code']] . '</td>';
                    }
                    $aaData[$key][] = '<td> <a href="' . admincp_url() . 'translate/edit/' . $value['tran_id'] . '" class="btn btn-default tip" title="Edit"><i class="fa '.$this->config->item('button_edit').'"></i></a>
                                            <a href="javascript:void(0)" title="Delete" class="btn btn-default tip" onclick="confirm_delete('.$value['tran_id'].',\''.$this->router->fetch_class().'\');"><i class="fa '.$this->config->item('button_delete').'"></i></a></td>';
                }
                $result = array(
                	"aaData" => $aaData,
                	"iTotalDisplayRecords" => $rows['total']
            	);
            }
            $this->output->set_output(json_encode($result));
        }else{
            $data['icon_page']=$this->config->item('icon_view');
            $data['title']='Translate Manager';
            $data['sub_title']='Translate Manager';
            $data['info'] = $dataLang;
        	$this->parser->parse("translate/view.tpl", NULL, $data);
        }
    }

    public function add(){
        $data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Translate';
        $data['sub_title']='Create New Translate';
        $dataLang = $this->registry->get('language');
        $data['language'] = $dataLang;
		if($this->input->server('REQUEST_METHOD') == "POST"){
            $data_insert['keyword'] = $this->input->post('keyword');
            foreach($dataLang as $lang){
                $data_insert['translate_'.$lang['lang_code']] = $this->input->post('translate_'.$lang['lang_code']);
            }
            $this->Mtranslate->insert($data_insert);
            $this->render_translate(TRUE, TRUE);
            $this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
            redirect(admincp_url()."translate/view");
        }
        $this->breadcrumbs->push('Create Translate', 'admincp_url/translate/add');
		$this->parser->parse("translate/add.tpl", NULL, $data);
    }

    public function edit(){
        $data['icon_page']=$this->config->item('icon_edit');
        $data['title_page']='Edit Translate';
        $data['sub_title_page']='Edit Translate';
        $dataLang = $this->registry->get('language');
        $data['language'] = $dataLang;
    	$cache_id = $id = $this->uri->segment(4);
    	$data['info'] = $this->Mtranslate->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_update['keyword'] = $this->input->post('keyword');
            foreach($dataLang as $lang){
                $data_update['translate_'.$lang['lang_code']] = $this->input->post('translate_'.$lang['lang_code']);
            }
			$this->Mtranslate->update($data_update,$id);
            $this->render_translate(TRUE, TRUE);
			$this->session->set_flashdata("success", trans('message_edit_successfully'));
			redirect(admincp_url()."translate/view");
		}
        $this->breadcrumbs->push('Edit Translate', 'admincp_url/translate/edit/'.$id);
		$this->parser->parse("translate/edit.tpl", $cache_id, $data);
    }

    public function getTrans() {
        if ($this->input->is_ajax_request()) {
            $word = $this->input->post('word');
            echo $this->translate->get($word);
        }
    }
	
	public function delete(){
		$id = $this->uri->segment(4);
		$this->Mtranslate->delete($id);
        $this->render_translate(TRUE, TRUE);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."translate/view");
	}
}